﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace WPFDemo
{
    public class XmlHelper
    {


        public void CreateXml()
        {
            string _conn = "ss";
            //添加描述信息
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            //创建根节点
            XmlElement _setup = doc.CreateElement("Setup");
            doc.AppendChild(_setup);
            //给根节点创建子节点
            XmlElement conn = doc.CreateElement("conn");
            conn.InnerText = _conn;
            _setup.AppendChild(conn);
            doc.Save("setup.xml");
        }

        public void GetXml( string xml)
        {
            //获取xml
            xml = "D:\\Project\\数据管理系统\\DataManagementBoard\\WPFDemo\\Demo.xml";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            //获取根节点
            string Code = doc.SelectSingleNode("employees").InnerText.Trim();
            XmlNode parentNode = doc.SelectSingleNode("employees");
            XmlElement pn = (XmlElement)(parentNode);
            string p =pn.GetAttribute("Type");

            //获取节点的有序集合
            XmlNodeList xmlNodeList = doc.SelectNodes("employees/employee");
            List<User> users = new List<User>();
            foreach (XmlNode item in xmlNodeList)
            {
                XmlElement xe=(XmlElement)item;
                string a=xe.GetAttribute("Type");
                User user = new User();
                user.Id = Convert.ToInt32(item.SelectSingleNode("Id").InnerText.Trim());
                user.Name = item.SelectSingleNode("Name").InnerText.Trim();
                user.Password = item.SelectSingleNode("Password").InnerText.Trim();
                users.Add(user);
            }
        }

    }

    public class User
    {
        public int Id {  get; set; }    
        public string Name { get; set; }
        public string Password { get; set; }
    }
}
